package com.kingint.web.admin.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kingint.common.unitedresult.K;
import com.kingint.mbg.bas.entity.BasItemsLeft;
import com.kingint.mbg.bas.entity.BasRoomState;
import com.kingint.mbg.bas.mapper.BasItemsLeftMapper;
import com.kingint.mbg.sys.entity.UmsMember;
import com.kingint.web.admin.base.service.BasItemsLeftService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kingint.web.sys.service.UmsAdminService;
import com.kingint.web.util.tree.TreeBo;
import com.kingint.web.util.tree.TreeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;

/**
 * <p>
 * 遗留物品相关设置表 服务实现类
 * </p>
 *
 * @author wxh
 * @since 2020-12-22
 */
@Service
@Slf4j
public class BasItemsLeftServiceImpl extends ServiceImpl<BasItemsLeftMapper, BasItemsLeft> implements BasItemsLeftService {

    @Resource
    private UmsAdminService adminService;
    @Resource
    private BasItemsLeftMapper basItemsLeftMapper;

    @Override
    public int insertItemsLeftInfo(BasItemsLeft basItemsLeft) {
        UmsMember currentMember = adminService.getCurrentMember();
        Integer hotelId = currentMember.getHotelId();
        if(StringUtils.isEmpty(hotelId)){
            log.debug("对应账号没有关联酒店id");
            return 0;
        }
        basItemsLeft.setCreateTime(new Date());
        basItemsLeft.setHotelId(hotelId);
        int insert = basItemsLeftMapper.insert(basItemsLeft);
        return insert;
    }

    @Override
    public int updateItemsLeftInfo(BasItemsLeft basItemsLeft) {
        int i = basItemsLeftMapper.updateById(basItemsLeft);
        return i;
    }

    @Override
    public int deleteItemsLeftInfo(Integer id) {
        UmsMember currentMember = adminService.getCurrentMember();
        Integer hotelId = currentMember.getHotelId();
        QueryWrapper<BasItemsLeft> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("pid",id).eq("hotel_id", hotelId);
        int count = this.count(queryWrapper);
        if(count>0){
            return -1;
        }
        BasItemsLeft left = new BasItemsLeft();
        left.setId(id);
        left.setStatus(1);//删除
        int i = basItemsLeftMapper.updateById(left);
        return i;
    }

    @Override
    public Object getItemsLeftList(BasItemsLeft basItemsLeft, Integer pageNum, Integer pageSize) {
        UmsMember currentMember = adminService.getCurrentMember();
        Integer hotelId = currentMember.getHotelId();
        if(StringUtils.isEmpty(hotelId)){
            log.debug("对应账号没有关联酒店id");
            return null;
        }
//        IPage<BasItemsLeft> page = new Page<>(pageNum,pageSize);
//        QueryWrapper<BasItemsLeft> wrapper = new QueryWrapper<>();
//        wrapper.eq("hotel_id",hotelId)
//                .eq("status",0)
//                .eq("pid",0);
//        IPage<BasItemsLeft> iPage = basItemsLeftMapper.selectPage(page, wrapper);
//        QueryWrapper<BasItemsLeft> wrapper1 = new QueryWrapper<>();
//        wrapper1.eq("hotel_id",hotelId)
//                .eq("status",0);
//        List<BasItemsLeft> basItemsLefts = basItemsLeftMapper.selectList(wrapper1);
//        cancelItemsLeft(iPage.getRecords(),basItemsLefts);
//        return iPage;
        QueryWrapper<BasItemsLeft> wrapper = new QueryWrapper<>();
        wrapper.eq("hotel_id", hotelId)
                .eq("status", 0);
        List<BasItemsLeft> list = this.list(wrapper);
        TreeUtil<BasItemsLeft> basGoodsTreeUtil = new TreeUtil<BasItemsLeft>("getId","getPid");

        List<TreeBo<BasItemsLeft>> createTreeAsTreeEntity=null;
        List<BasItemsLeft> list2=new ArrayList<>();
        try {

            createTreeAsTreeEntity = basGoodsTreeUtil.createTreeAsTreeEntity(new HashSet<BasItemsLeft>(list));
            if(createTreeAsTreeEntity.size()>0)
                tree(list2,createTreeAsTreeEntity);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return K.ok().data(list2);
    }
    private void  tree(List<BasItemsLeft> list,List<TreeBo<BasItemsLeft>>  TreeBo){
        for (TreeBo<BasItemsLeft> itemsTreeBo : TreeBo) {
            BasItemsLeft title = itemsTreeBo.getTitle();
            List<TreeBo<BasItemsLeft>> children = itemsTreeBo.getChildren();
            if(children!=null){
                tree(title.getChildren(),children);
            }
            list.add(title);
        }

    }
    public void cancelItemsLeft(List<BasItemsLeft> aClass, List<BasItemsLeft> allList) {
        if (!CollectionUtils.isEmpty(aClass)) {
            //为一级菜单设置子菜单
            for (BasItemsLeft info : aClass) {
                getChildList(info, allList);
            }
        }
    }

    public void getChildList(BasItemsLeft info, List<BasItemsLeft> aClass) {
        List<BasItemsLeft> serviceClass = info.getItemsLeftList();
        if (CollectionUtils.isEmpty(serviceClass)) {
            serviceClass = new ArrayList<>();
        }
        for (BasItemsLeft ser : aClass) {
            if (ser.getPid() == info.getId()) {
                serviceClass.add(ser);
            }
        }
        //递归查询
        if (serviceClass.size() != 0) {
            for (BasItemsLeft child : serviceClass) {
                getChildList(child, aClass);
            }
        }
        info.setItemsLeftList(serviceClass);
    }
}
